var BaseSetting = app.controller('BaseSetting', function($rootScope, $scope, $http,$state,$cordovaChat,BaseSettingData) {
	try{
		try{
			Wechat.isInstalled({scope: 'snsapi_userinfo'}, 
					function onSuccess(suc) {
				$rootScope.inswx=true;
			},
			function onError(err) {
				$rootScope.inswx=false;
			}
			);

			YCQQ.checkClientInstalled(
					function onSuccess(suc) {
						$rootScope.insqq=true;
					},
					function onError(err) {
						$rootScope.insqq=false;
					}
			);
		}catch (e) {
		}
		
		$rootScope.showf=false;

		$scope.info={};
		$scope.tel='';

		var url=SITE_URL+"index.php?app=baseloginapp&act=userinfo&callback=JSON_CALLBACK";
		$http.jsonp(url).success(function(data) {
			if(data.done){
				$scope.info=data.retval;
				if($scope.info!=null&&$scope.info.phone_tel!=null&&$scope.info.phone_tel!=""){
					var temptel=$scope.info.phone_tel.substr(3,4);
					$scope.tel=$scope.info.phone_tel.replace(temptel,"****");
				}else{
					$scope.tel='请绑定手机';
				}
			}
		});

		$scope.logout = function(e){
			if(e){
				e.stopPropagation();
			}
			try{
				var url=SITE_URL+"index.php?app=usermyapp&act=logout&callback=JSON_CALLBACK";
				$http.jsonp(url).success( function(data) {
					if(data.done){
						window.localStorage.u="";
						window.localStorage.p="";
						window.localStorage.user="";

						$rootScope.istutor = false;
						if ($cordovaChat.ChatConnected()) {
							$cordovaChat.ChatDisConnect();
						}

						$rootScope.go("loginmain");
					}
				});
			}
			catch(e){showcatch(e);}
		};
		
		//QQ绑定
		$scope.qqlogintimer = null;
		$scope.bindqq=function(){
			if(!$rootScope.insqq){
				showtoast("您还没有安装QQ");
				return;
			}
			
			ProgressIndicator.showSimpleWithLabel(false, '正在启动QQ...');
			$scope.qqlogintimer = window.setTimeout("ProgressIndicator.hide()", 3000);
			var checkClientIsInstalled = 1;//default is 0,only for iOS
			YCQQ.ssoLogin(function(args){
				/*
				 *
				 Object
				 access_token: "8D31F3B30ED7638006E4E026F828605B"
				 userid: "A1F5CBF8EBB5CDEA76300D135EE446DA"
				 * */
				console.log(args);
				if ($scope.qqlogintimer != null)
					window.clearTimeout($scope.qqlogintimer);
				ProgressIndicator.hide();
				ProgressIndicator.showSimpleWithLabel(false, '正在获取QQ信息...');
				
				var url = SITE_URL + "index.php?app=userapp&act=qqbind&callback=JSON_CALLBACK&t=" + args.access_token+"&id="+args.userid;
                $http.jsonp(url).success(function (data) {
                    if (data.done) {
                    	ProgressIndicator.hide();
                    	$state.reload();
                    }
                    else {
                        ProgressIndicator.hide();
                        console.log("QQ绑定失败，请重试4");
                    }
                }).error(function () {
                    ProgressIndicator.hide();
                    console.log("QQ绑定失败，请重试5");
                });
                console.log("qqlogin suc", suc);

			},function(failReason){
				console.log(failReason);
				 if ($scope.qqlogintimer != null)
                     window.clearTimeout($scope.qqlogintimer);
                 ProgressIndicator.hide();
                 console.log("绑定失败" + failReason);
			},checkClientIsInstalled);
		};
		
		//微信用户绑定
        $scope.wxlogintimer = null;
        $scope.bindwx = function () {
        	if(!$rootScope.inswx){
				showtoast("您还没有安装微信");
				return;
			}
            ProgressIndicator.showSimpleWithLabel(false, '正在启动微信...');
            $scope.wxlogintimer = window.setTimeout("ProgressIndicator.hide()", 3000);
            Wechat.auth({scope: 'snsapi_userinfo'},
                function onSuccess(suc) {
                    if ($scope.wxlogintimer != null)
                        window.clearTimeout($scope.wxlogintimer);
                    ProgressIndicator.hide();
                    ProgressIndicator.showSimpleWithLabel(false, '正在获取微信信息...');

                   
                    var url = SITE_URL + "index.php?app=userapp&act=wxbind&callback=JSON_CALLBACK&code=" + suc.code;
                    $http.jsonp(url).success(function (data) {
                        if (data.done) {
                        	ProgressIndicator.hide();
                        	$state.reload();
                        }
                        else {
                            ProgressIndicator.hide();
                            console.log("微信绑定失败，请重试4");
                        }
                    }).error(function () {
                        ProgressIndicator.hide();
                        console.log("微信绑定失败，请重试5");
                    });
                    console.log("wxlogin suc", suc);
                },
                function onError(err) {
                    if ($scope.wxlogintimer != null)
                        window.clearTimeout($scope.wxlogintimer);
                    ProgressIndicator.hide();
                    /*
                     * wxlogin err ERR_USER_CANCEL
                     * */
                    console.log("绑定失败" + err);
                });
        };
	}
	catch (e) {
		showcatch(e);
	}
});
BaseSetting.BaseSettingData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}